package com.originskyseed.platform.user.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.model.GiftRelateReceive;
import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.ExportExcelUtil;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.Player;
import com.originskyseed.platform.user.model.TaskSelectPlayerVO;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IFamilyService;
import com.originskyseed.platform.user.service.IPlayerRelateLoginService;
import com.originskyseed.platform.user.service.IPlayerService;
import com.originskyseed.platform.user.service.ITradeUnionService;
import com.originskyseed.platform.user.service.IUserInfoService;
import com.originskyseed.platform.user.vo.request.PlayerRequestVO;

/**
 * 玩家信息表Controller类（OSI_PLAYER）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/player")
@BaseLocaleResource(localeFileName = "i18n_user")
public class PlayerController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PlayerController.class);

	@DubboReference
	private IPlayerService playerService;

	@DubboReference
	private IFamilyService familyService;

	@DubboReference
	private IPlayerRelateLoginService playerRelateLoginService;

	@DubboReference
	private ITradeUnionService tradeUnionService;

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;


	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Player>> playerList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Player>> result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);

			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			if (userInfo.getType() == 6 || userInfo.getType() == 11) {
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			} else {
				Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
				List<String> deptList = new ArrayList<>();
				deptList.add(userInfo.getDeptId());
				if (dept != null) {
					selectSonId(dept.getCode(), deptList);
					otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList,2);
				}
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			}
			if(otherAccountList.size()==0){
				return new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), new BasePage(requestVO.getPageNo(), requestVO.getPageSize()));
			}

			requestVO.setChannelSubIdList(otherAccountList);
			// 分页查询
			BasePage<Player> page = new BasePage<Player>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 选择维护用户（玩家）信息列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectMaintainPlayerList.json")
	@RequiredParams(names = { "pageNo", "pageSize", "type" }) // 1主播 2业务员
	public BaseResponseBody<BasePage<TaskSelectPlayerVO>> selectMaintainPlayerList(HttpServletRequest request) {
		BaseResponseBody<BasePage<TaskSelectPlayerVO>> result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 分页查询
			BasePage<TaskSelectPlayerVO> page = new BasePage<TaskSelectPlayerVO>(requestVO.getPageNo(), requestVO.getPageSize());
			List<String> userIdList = new ArrayList<>();
			if (userInfo.getSourceSys() == 0) {
				// 查询所有部门
				Dept dept = new Dept();
				List<String> deptIdList = new ArrayList<>();
				if (requestVO.getType() == 1) {
					dept.setSourceSys(1);
					List<Dept> deptList = this.deptService.selectList(dept);
					for (Dept dept1 : deptList) {
						if (dept1.getType() == 3) {
							deptIdList.add(dept1.getId());
						}
					}
					userIdList = this.userInfoService.selectAnchorOtherAccountsByDeptIds(deptIdList);
					requestVO.setUserIdList(userIdList);
					// 查主播
					page = this.playerService.taskSelectPlayerToAnchorListByPage(requestVO.toMap(), page);
				} else {
					dept.setSourceSys(2);
					List<Dept> deptList = this.deptService.selectList(dept);
					for (Dept dept1 : deptList) {
						if (dept1.getType() == 5) {
							deptIdList.add(dept1.getId());
						}
					}
					userIdList = this.userInfoService.selectSalesmanUserIdsByDeptIds(deptIdList);
					requestVO.setUserIdList(userIdList);
					// 查业务员
					page = this.playerService.taskSelectPlayerToSalerListByPage(requestVO.toMap(), page, userInfo);
				}
			} else {
				List<TaskSelectPlayerVO> list = new ArrayList<>();
				if (userInfo.getType() != 5 || userInfo.getType() != 11) {
					// 查询所有的子节点
					List<String> deptIdList = new ArrayList<>();
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					if (dept != null) {
						// 如果是小组
						if ((dept.getSourceSys() == 2 && dept.getType() == 5) || (dept.getSourceSys() == 1 && dept.getType() == 3)) {
							deptIdList.add(dept.getId());
						} else {
							this.selectSonId(dept.getCode(), deptIdList);
							deptIdList.add(dept.getId());
						}
					}
					if (userInfo.getSourceSys() == 1) { // 主播
						userIdList = this.userInfoService.selectAnchorOtherAccountsByDeptIds(deptIdList);
						requestVO.setUserIdList(userIdList);
						page = this.playerService.taskSelectPlayerToAnchorListByPage(requestVO.toMap(), page);
					} else {
						userIdList = this.userInfoService.selectSalesmanIdsByDeptIds(deptIdList);
						requestVO.setUserIdList(userIdList);
						page = this.playerService.taskSelectPlayerToSalerListByPage(requestVO.toMap(), page, userInfo);
					}
				}

			}
			result = new BaseResponseBody<BasePage<TaskSelectPlayerVO>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<TaskSelectPlayerVO>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 用户注册登录信息_分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerLoginList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Player>> playerLoginList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Player>> result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// TradeUnion tradeUnion = this.tradeUnionService.selectByUserId(userInfo.getId());
			// if(tradeUnion != null){
			// requestVO.setTradeUnionId(tradeUnion.getId());
			// }
			if (userInfo.getDeptId() != null) {
				requestVO.setTradeUnionId(userInfo.getDeptId());
				// 分页查询
				BasePage<Player> page = new BasePage<Player>(requestVO.getPageNo(), requestVO.getPageSize());
				page = this.playerService.selectLoginListByPage(requestVO.toMap(), page);
				result = new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 用户注册登录信息 导出Excel
	 *
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/selectLoginListToExcel.json")
	@SysLog(module = "用户注册登录信息", operationDesc = "用户注册登录信息导出excel")
	public void selectLoginListToExcel(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Player> list = new ArrayList<>();
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// TradeUnion tradeUnion = this.tradeUnionService.selectByUserId(userInfo.getId());
			if (userInfo.getDeptId() != null) {
				requestVO.setTradeUnionId(userInfo.getDeptId());
				list = this.playerService.selectLoginListToExcel(requestVO.toMap());
			}
			// excel标题
			String[] title = { "账号", "设备", "子渠道", "主渠道", "注册时间", "登录次数", "设备码", "IP", "IMEI" };
			// excel文件名
			String fileName = "用户注册登录信息.xls";
			// sheet名
			String sheetName = "用户注册登录信息";
			String[][] values = new String[list.size()][];
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					values[i] = new String[title.length];
					Player obj = list.get(i);
					values[i][0] = obj.getAccount() == null ? "" : obj.getAccount();
					values[i][1] = obj.getEquipment() == null ? "" : obj.getEquipment();
					values[i][2] = obj.getCsaccount() == null ? "" : obj.getCsaccount();
					values[i][3] = obj.getCaccount() == null ? "" : obj.getCaccount();
					values[i][4] = obj.getRegistTime() == null ? "" : formatter.format(obj.getRegistTime());
					values[i][5] = obj.getLoginCount() == null ? "" : obj.getLoginCount().toString();
					values[i][6] = obj.getLoginCode() == null ? "" : obj.getLoginCode();
					values[i][7] = obj.getIp() == null ? "" : obj.getIp();
					values[i][8] = obj.getImei() == null ? "" : obj.getImei();
				}
			}
			outPutExcel(response, title, fileName, sheetName, values);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 输出Excel
	 *
	 * @param response
	 * @param title
	 * @param fileName
	 * @param sheetName
	 * @param values
	 * @throws IOException
	 */
	private void outPutExcel(HttpServletResponse response, String[] title, String fileName, String sheetName, String[][] values) throws IOException {
		// 创建HSSFWorkbook
		HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(sheetName, title, values, null);
		// 将文件存到指定位置
		this.setResponseHeader(response, fileName);
		OutputStream os = response.getOutputStream();
		wb.write(os);
		os.flush();
		os.close();
	}

	/**
	 * 发送响应流方法
	 *
	 * @param response
	 * @param fileName
	 */
	public void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage(), ex);
		}
	}

	/**
	 * 集成_玩家信息列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectPlayerList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Player>> selectPlayerList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Player>> result = null;
		BasePage<Player> page = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			page = new BasePage<Player>(requestVO.getPageNo(), requestVO.getPageSize());
			if (userInfo.getSourceSys() == 2) { // 业务员
				// 分页查询
				page = this.playerService.selectPlayerListByPage(requestVO.toMap(), page, userInfo);
			} else if (userInfo.getSourceSys() == 1) { // 主播
				page = this.playerService.selectPlayerListToAnchorByPage(requestVO.toMap(), page, userInfo);
			} else if (userInfo.getSourceSys() == 0) { // ow
				page = this.playerService.selectPlayerListToOWByPage(requestVO.toMap(), page, userInfo);
			}
			result = new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 集成_用户流量池
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectPlayerPlowPoolList.json")
	@RequiredParams(names = { "pageNo", "pageSize", "type" }) // type: 1、公域 2、私域
	public BaseResponseBody<BasePage<Player>> selectPlayerPlowPoolList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Player>> result = null;

		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			BasePage<Player> page = null;
			page = new BasePage<Player>(requestVO.getPageNo(), requestVO.getPageSize());
			if (userInfo.getSourceSys() == 2) { // 业务员
				if (requestVO.getType() != null && requestVO.getType() == 2) { // 私域
					// 分页查询
					requestVO.setBelongBusinessId(userInfo.getOtherAccount());
					page = this.playerService.selectPlayerPlowPoolListByPage(requestVO.toMap(), page);
				} else {
					// 公域
					page = this.playerService.selectPlayerPublicPlowPoolListByPage(requestVO.toMap(), page, userInfo);
				}
			} else if (userInfo.getSourceSys() == 1) { // 主播-公域
				page = this.playerService.selectPlayerPlowPoolListToAnchorByPage(requestVO.toMap(), page, userInfo);
			}
			result = new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerSave.json")
	@RequiredParams(names = {})
	public BaseResponseBody<Player> playerSave(HttpServletRequest request) {
		BaseResponseBody<Player> result = null;
		try {
			// 请求对象
			Player player = this.getParameterBean(Player.class);

			if (StringUtils.isNotBlank(player.getId())) {
				this.playerService.updateByPrimaryKey(player);
			} else {
				this.playerService.insertSelective(player);
			}
			result = new BaseResponseBody<Player>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), player);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Player>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerDelete.json")
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> playerDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.playerService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询用户明细列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectUserDetails.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody selectUserDetails(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
            Long start=System.currentTimeMillis();
			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					deptList.add(userInfo.getDeptId());
					if (dept != null) {
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 2);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {
					return new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), new BasePage(requestVO.getPageNo(), requestVO.getPageSize()));
				}
				requestVO.setChannelSubIdList(otherAccountList);
			}
			// 分页查询
			BasePage<Player> page = new BasePage(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerService.selectUserDetailsByPage(requestVO.toMap(), page);
			logger.info("==selectUserDetails==end=="+(System.currentTimeMillis()-start)+"==ms=");
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询优质用户列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectHighQualityUser.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody selectHighQualityUser(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);

			// 分页查询
			BasePage<Player> page = new BasePage(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerService.selectHighQualityUserByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询活跃登录用户列表，调另外一个接口了
	 *
	 * @param request
	 * @return
	 */
	/*@ResponseBody
	@RequestMapping(value = "/selectActiveLoginUser.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody selectActiveLoginUser(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);
			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if (userInfo.getType() == 6 || userInfo.getType() == 11 ) {
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			} else {
				Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
				List<String> deptList = new ArrayList<>();
				if (dept != null) {
					selectSonId(dept.getCode(), deptList);
					otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList,2);
				}
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			}
			if(otherAccountList.size()==0){
				return new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), new BasePage(requestVO.getPageNo(), requestVO.getPageSize()));
			}
			requestVO.setChannelSubIdList(otherAccountList);
			// 分页查询
			BasePage<Player> page = new BasePage(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerService.selectActiveLoginUserByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}*/

	/**
	 * 根据玩家id查询玩家信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectByPlayerId.json")
	@RequiredParams(names = { "playerId" })
	public BaseResponseBody selectByPlayerId(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 玩家id
			String playerId = this.getParameterString("playerId", null);

			Player player = this.playerService.selectByPlayerId(playerId);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), player);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectRegisterList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Player>> selectRegisterList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Player>> result = null;
		try {
			// 请求对象
			PlayerRequestVO requestVO = this.getParameterBean(PlayerRequestVO.class);

			// 获取当前登录用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if(userInfo==null)
			{
				result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()),null, null);
				return result;
			}
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			// 分页查询
			BasePage<Player> page = new BasePage<Player>(requestVO.getPageNo(), requestVO.getPageSize());

			// 根据范围查询
			List<String> otherAccountList = new ArrayList<>();
			if (userInfo.getType() == 6 || userInfo.getType() == 11 ) {
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			} else {
				Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
				List<String> deptList = new ArrayList<>();
				deptList.add(userInfo.getDeptId());
				if (dept != null) {
					selectSonId(dept.getCode(), deptList);
					otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList,2);
				}
				if(StringUtils.isNotBlank(otherAccount)) {
					otherAccountList.add(otherAccount);
				}
			}
			if(otherAccountList.size()==0){
				return new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
			}
			requestVO.setChannelSubIdList(otherAccountList);
			System.out.println("===requestVO="+requestVO);
			page = this.playerService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Player>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Player>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询组织下的部门
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}

	/**
	 * 查询组织架构接口-所有的主播
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectGroupIdAnchor(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				if (dept1.getType() == 3) {
					list.add(dept1.getId());
				}
				selectGroupIdAnchor(dept1.getCode(), list);
			}
		}
	}

	/**
	 * 查询组织架构接口-所有的业务员
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectGroupIdSalesMan(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				if (dept1.getType() == 5) {
					list.add(dept1.getId());
				}
				selectGroupIdSalesMan(dept1.getCode(), list);
			}
		}
	}

}
